import RemoteDAO from './RemoteDAO'

export function buryPoint (eventCode, kvFun) {
  return function (target, key, descriptor) {
    const method = descriptor.value
    descriptor.value = function () {
      const kv = kvFun.apply(this)
      let TDAPP = window.TDAPP
      TDAPP.onEvent(eventCode, '', Object.assign({}, kv, {
        usercode: localStorage.getItem('userCode')
      }))
      TDAPP.send()
      console.info('埋点', eventCode, kv)
      return method.apply(this, arguments)
    }

    return descriptor
  }
}

export function checkLogin (target, key, descriptor) {
  const method = descriptor.value
  descriptor.value = function () {
    if (!localStorage.getItem('userCode')) {
      RemoteDAO.callNativeLogin(function (response) {
        return method.apply(this, arguments)
      })
    } else {
      return method.apply(this, arguments)
    }
  }

  return descriptor
}
